<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="线路名称"  class="filter-item search-input" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.title" placeholder="出发城市"  class="filter-item  search-input" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.title" placeholder="目的城市"  class="filter-item  search-input" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.importance" placeholder="线路状态" clearable  class="filter-item  search-input">
        <el-option v-for="item in importanceOptions" :key="item" :label="item.value" :value="item.key" />
      </el-select>
      <el-input v-model="listQuery.title" hidden placeholder="分公司ID" class="filter-item  search-input" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <!-- <el-table-column label="线路主键ID" prop="id" sortable="custom" align="center" width="80" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="线路主键ID" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="线路名称" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="线路状态" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="线跌价格" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出发城市" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.startCity }}</span>
        </template>
      </el-table-column>
      <el-table-column label="目的城市" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.endCity }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分公司名" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.peer }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { fetchList, fetchPv, createArticle, updateArticle } from '@/api/lineManagement'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        ID:undefined,
        title: undefined,
        status: undefined,
        price: undefined,
        startCity: undefined,
        endCity: undefined,
        peer: undefined
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作Success',
        type: 'success'
      })
      row.status = status
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
  }
}
</script>

<style>
  .search-input{
    width: 150px;
    margin-right: 15px;
  }
</style>
